home *** CD-ROM | disk | FTP | other *** search
/ 5 Star Games: DOS Edition 2 / 5 Star Games - DOS Edition (1995)(Ready to Run).iso / dbc / db_gvar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  4.6 KB  |  187 lines

  1. /****************************************************************************/
  2. /*                         DATABOSS MODULE: DB_GVAR                         */
  3. /*                                                                          */
  4. /*  Please update this commentary when changes have been made               */
  5. /*                                                                          */
  6. /*                                                                          */
  7. /*   Date     Version     Person       Description                          */
  8. /* ------------------------------------------------------------------------ */
  9. /*  24/05/91    3.51        SN         Added code to 'initThisProg' to      */
  10. /*                                     remove double backslashes '\'        */
  11. /****************************************************************************/
  12.  
  13. #include "db_lsc.h"
  14.  
  15.  
  16. #ifdef __TURBOC__
  17.     #include <conio.h>
  18. #else
  19.     #include <graph.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "db_types.h"
  25. #include "db_conio.h"
  26. #include "db_dos.h"
  27. #include "db_dmem.h"
  28. #include "db_file.h"
  29. #include "db_str.h"
  30. #include "db_gvar.h"
  31.  
  32. /****************************  GLOBAL CONSTANTS  ****************************/
  33.  
  34. const uchar recog[] = "DBGV";
  35.  
  36. /****************************  GLOBAL VARIABLES  ****************************/
  37.  
  38. bool i_alloc;
  39. grecptr dgvar;
  40. dbossgv *gvar;
  41. fspec thisprog;
  42. bool dbcaller;
  43. string lookalong;
  44.  
  45. /***************************  INTERNAL VARIABLES  ***************************/
  46.  
  47. static bool initialized = False;
  48.  
  49. /*****************************  IMPLEMENTATION  *****************************/
  50.  
  51. ptr string2pointer(str8 s)
  52. {
  53.     int i;
  54.     ptr p;
  55.     byte x[4];
  56.  
  57.     for (i=0; i<4; i++)
  58.         x[i] = (s[i*2] & 0x0F) | ((s[i*2+1] & 0x0F) << 4);
  59.     memmove(&p,x,4);
  60.     return (p);
  61. }
  62.  
  63. strptr pointer2string(strptr sout,ptr p)
  64. {
  65.     int i;
  66.     byte x[4];
  67.  
  68.     memmove(x,&p,4);
  69.     for (i=0; i<4; i++) {
  70.         sout[i*2] = (x[i] & 0x0F) | 0x30;
  71.         sout[i*2+1] = (x[i] >> 4) | 0x30;
  72.     }
  73.     sout[8] = 0;
  74.     return (sout);
  75. }
  76.  
  77. void pascal2c(string instr, int max)
  78. {
  79.     byte l;
  80.  
  81.    l = instr[0];
  82.     if (l <= ((byte)(max-1)))
  83.         memmove(instr,&instr[1],l);
  84.     else
  85.         l = 0;
  86.     instr[l] = '\0';
  87. }
  88.  
  89. void initthisprog(void)
  90. {
  91.     if (_osmajor >= 3)
  92.         strcpy(thisprog.fpath,paramstr(0));
  93.     else {
  94.       getcurrentdir(thisprog.fpath, 0);
  95.       if (*(thisprog.fpath + 2) == '\\') strcat(thisprog.fpath, "UKNOWN.EXE");
  96.       else                               strcat(thisprog.fpath, "\\UKNOWN.EXE");
  97.     }
  98.     fsplit(thisprog.fpath,thisprog.fdir,thisprog.fname,thisprog.fext);
  99. }
  100.  
  101. bool initgvar(idstr chkid, word size, grecptr *dgv, bool *ia)
  102. {
  103.     int i;
  104.     long tsz;
  105.     string ts,tmp;
  106.  
  107.     *dgv = NULL;
  108.     i = 1;
  109.     while ((*dgv == NULL) && (i < paramcount())) {
  110.         strcpy(ts,paramstr(i));
  111.         if (strcmp(strcopy(tmp,ts,0,strlen(recog)),recog) == 0) {
  112.             strdelete(ts,0,strlen(recog));
  113.             *dgv = string2pointer(ts);
  114.             memmove(ts,(*dgv)->id,sizeof((*dgv)->id));  pascal2c(ts,sizeof(ts));
  115.             if ((strcmp((*dgv)->id,chkid) == 0) || (strcmp(ts,chkid) == 0))
  116.                 *ia = False;
  117.             else
  118.                 *dgv = NULL;
  119.         }
  120.         i++;
  121.     }
  122.     if (*dgv == NULL) {
  123.         tsz = sizeof(grecord) + size - 1;
  124.         *dgv = dosmallochigh(tsz);
  125.         if (*dgv != NULL) {
  126.             strcpy((*dgv)->id,chkid);
  127.             (*dgv)->gsize = tsz;
  128.             *ia = True;
  129.         }
  130.     }
  131.     return ((bool) (*dgv != NULL));
  132. }
  133.  
  134. void initdboss(grecptr dgv)
  135. {
  136.     gvar = (dbossgv *) &(dgv->gdata);
  137.     if (i_alloc) {
  138.         strcpy(gvar->operator,"********");
  139.         strcpy(gvar->fname,thisprog.fname);
  140.         gvar->sec = 0;
  141.         strcpy(gvar->company,"Kedwell Software Pty. Ltd.");
  142.         gvar->prog[0] = '\0';
  143.         gvar->language = 'C';
  144.         dbcaller = False;
  145.     }
  146.     else {
  147.         dbcaller = True;
  148.         if (gvar->language == 'P') {
  149.             pascal2c(gvar->operator,sizeof(gvar->operator));
  150.             pascal2c(gvar->fname,sizeof(gvar->fname));
  151.             pascal2c(gvar->company,sizeof(gvar->company));
  152.             pascal2c(gvar->prog,sizeof(gvar->prog));
  153.             gvar->language = 'C';
  154.         }
  155.     }
  156. }
  157.  
  158. /**********************  UNIT INITIALIZATION/EXIT CODE  *********************/
  159.  
  160. void db_gvar_exit(void)
  161. {
  162.     if (i_alloc)
  163.         dosfree(dgvar);
  164. }
  165.  
  166. void db_gvar_init(void)
  167. {
  168.     string p1,p2;
  169.  
  170.     if (!initialized) {
  171.         initialized = True;
  172.         if (!(initgvar("DATABOSS",sizeof(dbossgv),&dgvar,&i_alloc))) {
  173.             cwrite(_NoGlobalSpace);
  174.          cwrite("\n\r");
  175.             exit(0);
  176.         }
  177.         else {
  178.             initthisprog();
  179.             initdboss(dgvar);
  180.             atexit(db_gvar_exit);
  181.         }
  182.         strconcat(lookalong,getenvstr(p1,"DBPATH"),";",getenvstr(p2,"PATH"),";",thisprog.fdir,NULL);
  183.     }
  184. }
  185.  
  186. /*****************************  END OF DB_GVAR.C  ****************************/
  187.